# -*- coding: utf-8 -*-

import sys, os
from engine import Engine


def init_engine():
    # 获取词表
    cur_dir = os.path.dirname(__file__)
    fname = os.path.join(cur_dir, 'book.txt')

    e = Engine(bf_size=1024)
    with open(fname, 'r') as fp:
        while True:
            line = fp.readline()
            if line == '':
                break
            e.add_event(line)

    return e


def main():
    if len(sys.argv) < 2:
        print('用法 python %s <要搜索的东西>' % sys.argv[0])
        return

    e = init_engine()
    for ln, res in e.search(' '.join(sys.argv[1:])):
        print(ln, res)


if __name__ == "__main__":
    main()
